package com.basic.library.utils.audio;

import android.content.Context;

import com.basic.library.utils.MyLog;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yj
 * @description
 */
public class MediaThreadPool {
    private static MediaThreadPool threadPool = new MediaThreadPool();
    private ExecutorService pool = Executors.newSingleThreadExecutor();
    boolean suspended = true;

    public static MediaThreadPool getInstance() {
        return threadPool;
    }

    public void execute(Context context, String filePathString) {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                suspended = true;
                MediaManager.getInstance().playSound(context, filePathString, new MediaManager.OnListener() {
                    @Override
                    public void onStart() {
//                        ToastCompat.show("一条语音播放开始");
                    }

                    @Override
                    public void onComplete() {
//                        ToastCompat.show("一条语音播放完成");
                        suspended = false;
                    }

                    @Override
                    public void onError(String e) {
//                        ToastCompat.show("一条语音播放失败" + e);
                        suspended = false;
                    }
                });
                while (suspended) {
                    try {
                        Thread.sleep(1000);
                        MyLog.p("等待");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
